package com.mjkj.demo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjkj.demo.core.redis.RedisCache;
import com.mjkj.demo.model.LoginUser;
import com.mjkj.demo.model.entity.User;
import com.mjkj.demo.model.req.LoginReq;
import com.mjkj.demo.model.req.RegisterReq;
import com.mjkj.demo.model.vo.LoginVo;
import com.mjkj.demo.service.UserService;
import com.mjkj.demo.mapper.UserMapper;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import com.mjkj.demo.utils.JwtUtil;
import javax.annotation.Resource;
import java.util.Objects;
import com.mjkj.demo.utils.RedisKeys;
/**
* @author koen
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-09-04 20:44:23
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{


	@Resource
	private RedisCache redisCache;

	@Resource
	private AuthenticationManager authenticationManager;

	@Override
	public LoginVo login(LoginReq requestParams) {
		// AuthenticationManager, princiapl -> username，credentials -> password
		UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(requestParams.getUsername(),requestParams.getPassword());
		Authentication authenticate = authenticationManager.authenticate(authenticationToken);
		//
		if(Objects.isNull(authenticate)){
			throw new RuntimeException("用户名或密码错误");
		}
		// 会把UserDetails放进principal里，强转就能拿到了（？
		LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
		String userId = loginUser.getUser().getUserId().toString();
		String token = JwtUtil.generateToken(userId);


		redisCache.setCacheObject(RedisKeys.getLoginKey(userId), loginUser);

		return LoginVo.builder()
				.token(token)
				.build();
	}

	@Override
	public void register(RegisterReq registerReq) {

	}


//	@Override
//	public LoginVo login(LoginReq requestParams) {
//		User user = lambdaQuery()
//				.eq(User::getUsername, requestParams.getUsername())
//				.eq(User::getIsDelete, LogicDeleteTypeEnum.NOT_LOGIC_DELETE.getCode())
//				.one();
//		if (user == null) {
//			throw new ServiceException(new ErrorCode(201, "账号不存在"));
//		}
//
//		user = lambdaQuery()
//				.eq(User::getUsername, requestParams.getUsername())
//				.eq(User::getPassword, requestParams.getPassword())
//				.eq(User::getIsDelete, LogicDeleteTypeEnum.NOT_LOGIC_DELETE.getCode())
//				.one();
//		if (user == null) {
//			throw new ServiceException(new ErrorCode(201, "密码错误"));
//		}
//
//	    StpUtil.login(user.getUserId());
//		redisCache.setCacheObject(
//				RedisKeys.getLoginRoleKey(user.getUserId().toString()),
//				user.getRole().toString(),
//				7, TimeUnit.DAYS);
//
//		return LoginVo.builder()
//				.token(StpUtil.getTokenValue())
//				.build();
//	}
//
//	@Override
//	public void register(RegisterReq requestParams) {
//		User user = lambdaQuery()
//				.eq(User::getUsername, requestParams.getUsername())
//				.eq(User::getIsDelete, LogicDeleteTypeEnum.NOT_LOGIC_DELETE.getCode())
//				.one();
//		if (user != null) {
//			throw new ServiceException(new ErrorCode(201, "注册失败，存在相同的账号"));
//		}
//
//		save(User.builder()
//				.username(requestParams.getUsername())
//				.password(requestParams.getPassword())
//				.build());
//	}
}




